/** 原点 2020年8月19日18:04:18 发送验证码*/
var timeClock;
var timer_num = $.cookie('times');
var clock = $('.time_clock');
var clock_two = $('.clock_two');
var clock_bank = $('.clock_bank');
var bank_pay = $('.bank_pay');
var text = clock.text();
if (timer_num > 0) {
    clock.removeClass('time_clock');
    sendCode();
}

function sendCode() {

    timer_num = $.cookie('times');
    console.log(timer_num)
    timeClock = setInterval(function () {
        timer_num--;
        $.cookie('times', timer_num)
        clock.html('(' + timer_num + 's)');
        if (timer_num === 0) {
            clearInterval(timeClock);
            clock.html(text);
            clock.addClass('time_clock');
            $.cookie('times', 0)
        }
    }, 1000)
}

clock.click(function () {

    if (timer_num > 0) {
        return false;
    } else {
        var val = $("#mobile").val();
        if (!val) {
            return false;
        }
        send_msg(val);
    }
});

clock_two.click(function () {
    var data = $(this).attr('data-id');
    var val = $(data).val();
    if (!val) {
        return false;
    }

    if(!$.cookie('times'+val) || $.cookie('times'+val) <= 0){
        send_msg2(val,$(this),'clock_two');
    }
})

clock_bank.click(function () {

    if (timer_num > 0) {
        return false;
    } else {
        var tel_no = $("#tel_no").val();
        var card_id = $("#card_id").val();
        var vip_code = $("#vip_code").val();
        var expiration = $("#expiration").val();
        // console.log(tel_no,card_id,vip_code,expiration);
        if (!tel_no) {
            return false;
        }
        send_bank(tel_no,card_id,vip_code,expiration);
    }
});


bank_pay.click(function () {

    if (timer_num > 0) {
        return false;
    } else {
        var id =   $("#bank option:selected").val();
        // console.log(id);return
        if (!id) {
            return false;
        }
        send_bank_pay(id);
    }
});

function sendCode2(mobile, that, lc) {

    timer_num = $.cookie('times' + mobile);
    timeClock = setInterval(function () {
        timer_num--;
        $.cookie('times', timer_num)
        that.html('(' + timer_num + 's)');
        if (timer_num === 0) {
            clearInterval(timeClock);
            that.html('获取验证码');
            that.addClass(lc);
            $.cookie('times' + mobile, 0)
        }
    }, 1000)
}

function send_msg2(mobile,that,lc) {
    let load = layer.load(2);
    $.ajax({
        type: 'GET',
        url: '/index/login/send',
        data: {mobile: mobile},
        dataType: "json",
        success: function (data) {
            layer.close(load);
            if (data.code == 1) {
                $.cookie('times' + mobile, 60);
                that.removeClass(lc);
                layer.msg(data.msg);
                sendCode2(mobile,that,lc);
            } else {
                layer.msg(data.msg);
            }
        }
    });
}


function send_msg(mobile) {
    let load = layer.load(2);
    $.ajax({
        type: 'GET',
        url: '/index/login/send.html',
        data: {mobile: mobile},
        dataType: "json",
        success: function (data) {
            layer.close(load);
            if (data.code == 1) {
                $.cookie('times', 60);
                clock.removeClass('time_clock');
                layer.msg(data.msg);
                sendCode();
            } else {
                layer.msg(data.msg);
            }
        }
    });
}

function send_bank(tel_no,card_id,vip_code,expiration) {
    let load = layer.load(2);
    $.ajax({
        type: 'POST',
        url: '/index/user/bind_bank_card.html',
        data: {tel_no:tel_no,card_id:card_id,vip_code:vip_code,expiration:expiration},
        dataType: "json",
        success: function (data) {
            layer.close(load);
            if (data.code == 1) {
                $.cookie('times', 10);
                clock.removeClass('time_clock');
                layer.msg(data.msg);
                $('#apply_id').val(data.date);
                sendCode();
            } else {
                layer.msg(data.msg);
            }
        }
    });
}

function send_bank_pay(id) {
    let load = layer.load(2);
    $.ajax({
        type: 'POST',
        url: '/index/order/bank_pay.html',
        data: {id:id},
        dataType: "json",
        success: function (data) {
            layer.close(load);
            if (data.code == 1) {
                $.cookie('times', 10);
                clock.removeClass('time_clock');
                layer.msg(data.msg);
                $('#req_seq_id').val(data.date);
                sendCode();
            } else {
                layer.msg(data.msg);
            }
        }
    });
}

/** 发送验证码end */